
from pathlib import Path
import pandas as pd
import csv
from .imneo4j import import_data
class Neo4jImporter:
    def __init__(self, uri, user, password, destination_path):
        self.uri = uri
        self.user = user
        self.password = password
        self.destination_path = Path(destination_path)

    def import_to_neo4j(self, collected_filenames):
        # Function to clean and properly format the string fields
        def clean_quotes(value):
            if isinstance(value, str):
        # Remove extra quotes and strip leading/trailing spaces
              value = value.strip().replace('""', '"').replace('"', '')
        # Ensure proper quoting for fields with commas or quotes
              if ',' in value or '"' in value:
                    value = f'"{value}"'
            return value
        # 清洗数据并转换为 CSV 文件
        for file_info_list in collected_filenames:
            if file_info_list:  # 检查列表是否为空
                file_info = file_info_list[0]  # 获取字典每一个列表的元素是一个字典
                filename = file_info.get("filename")
                full_path = file_info.get("full_path")
                if filename is not None and full_path is not None:
                    df = pd.read_parquet(full_path)
                    # 清理数据
                    df = df.apply(lambda x: x.apply(clean_quotes))
                    # 转换为 CSV 文件并保存到 destination_path
                    csv_filename = f"{filename.replace('.parquet', '.csv')}"
                    destination_file = self.destination_path / csv_filename
                    df.to_csv(destination_file, index=False, quoting=csv.QUOTE_MINIMAL)

        # 导入数据到 Neo4j
        import_data(self.uri, self.user, self.password)